package com.tjb.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjb.reggie.common.TjbException;
import com.tjb.reggie.entity.DishFlavor;
import com.tjb.reggie.entity.dto.DishDto;
import com.tjb.reggie.service.DishFlavorService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.tjb.reggie.entity.Dish;
import com.tjb.reggie.mapper.DishMapper;
import com.tjb.reggie.service.DishService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tjb
 * @version 1.0
 * @date 2022/8/25 12:05
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Resource
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时插入菜品对应的口味数据，需要操作两张表：dish ,dish_flavor
     *
     * @param dishDto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish中
        this.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历设置口味的菜单id
        Long id = dishDto.getId();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据菜品id，查询菜品基本信息和口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getDishWithFlavor(Long id) {
        DishDto dishDto = new DishDto();
        //菜品基本信息
        Dish dish = this.getById(id);
        //菜品口味信息，通过菜品id
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dish.getId() != null, DishFlavor::getDishId, dish.getId());
        //查询口味
        List<DishFlavor> flavorList = dishFlavorService.list(wrapper);
        //封装赋值
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(flavorList);
        //返回
        return dishDto;
    }

    /**
     * 更新菜品，同时更新菜品对应的口味数据，需要同时操作两张表：dish, dish_flavor
     *
     * @param dishDto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据--dishFlavor的delete操作
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(wrapper);
        //添加当前提交过来的口味数据--dishFlavor的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        Long dishDtoId = dishDto.getId();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDtoId);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除对应的菜品基本信息和菜品口味
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIdsWithFlavor(List<Long> ids) {
        //判断能删除吗
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId, ids);
        queryWrapper.eq(Dish::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0){
            throw new TjbException("该菜品正在售卖，请停止售卖后删除");
        }
        //删除菜品
        this.removeByIds(ids);
        //删除对应口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(wrapper);

    }
}
